package com.pro.service.imple;

import com.pro.mapper.JsCarouselMapper;
import com.pro.pojo.JsCarousel;
import com.pro.pojo.JsonData;
import com.pro.service.JsCarouselService;
import com.pro.utils.DateUtils;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.calendar.BaseCalendar;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @Author 祖铭泽
 * @Date 2022/1/7/0007
 * @description 轮播图 service实现类
 */
@Service
public class JsCarouselServiceImple implements JsCarouselService {
    @Autowired
    private JsCarouselMapper jsCarouselMapper;
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;
    /**
     * @Author 祖铭泽
     * @Date 2022/1/7/0007
     * @description 根据上线/下线状态、图片类型获取轮播图
     */
    @Override
    public JsonData getBannersByDownType(JsCarousel jsCarousel) {
        List<JsCarousel> bannersByDownType = jsCarouselMapper.getBannersByDownType(jsCarousel);
        return bannersByDownType != null ? JsonData.buildSuccess(bannersByDownType) : JsonData.buildError("服务器出错了");
    }

    /**
     * @Author 祖铭泽
     * @Date 2022/1/7/0007
     * @description 根据id删除轮播图
     */
    @Override
    public JsonData delBannerById(Integer carouselId) {
        boolean delBannerById = jsCarouselMapper.delBannerById(carouselId);
        return delBannerById ? JsonData.buildSuccess(delBannerById) : JsonData.buildError("服务器出错了");
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/11/0007
     * @description 添加轮播图
     */
    @Override
    public boolean addBanner(JsCarousel jsCarousel) {
        return jsCarouselMapper.addBanner(jsCarousel);
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/12/0007
     * @description 根据id查询轮播图
     */
    @Override
    public JsonData getBannerMessageById(Integer carouselId) {
        JsCarousel bannerMessageById = jsCarouselMapper.getBannerMessageById(carouselId);
        return bannerMessageById != null ? JsonData.buildSuccess(bannerMessageById) : JsonData.buildError("服务器出错了") ;
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/12/0007
     * @description 根据id修改轮播图
     */
    @Override
    public JsonData updateBannerMessage(JsCarousel jsCarousel) {
        //判断是否是新上传的视频和图片 如果不是就删除
        if(!jsCarousel.getImgAddress().equals(jsCarousel.getOldImgAddress())){
            fastDFSClientUtil.delFile(jsCarousel.getOldImgAddress());
        }
        if(!jsCarousel.getVideoAddress().equals(jsCarousel.getOldVideoAddress())){
            fastDFSClientUtil.delFile(jsCarousel.getOldVideoAddress());
        }
        jsCarousel.setUpdateDate(DateUtils.tranferDateTimeToString(new Date()));
        boolean updateBannerMessage = jsCarouselMapper.updateBannerMessage(jsCarousel);
        return updateBannerMessage ? JsonData.buildSuccess(updateBannerMessage) : JsonData.buildError("修改失败");
    }
    /**
     * @Author 祖铭泽
     * @date 2022/1/21/0007
     * @description 查询即将到期的轮播图数量
     */
    @Override
    public JsonData getDueSoonBannersCount() {
        String[] dueSoonBannersCount = jsCarouselMapper.getDueSoonBannersCount();
        int count = 0;
        long currentTime = new Date().getTime();
        for (int i = 0; i < dueSoonBannersCount.length; i++) {
            try {
                long bannerExpireDate = DateUtils.tranferStringToDate(dueSoonBannersCount[i]).getTime();
                if(currentTime - bannerExpireDate < 0){
                    continue;
                }else if(currentTime - bannerExpireDate <= 86400000){
                    count++;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dueSoonBannersCount != null ? JsonData.buildSuccess(count) : JsonData.buildError("获取失败");
    }


}
